package week1exercises;

import java.util.ArrayList;

public class PrimeFactors {

    public static ArrayList<Integer> generate(int n) {
        /*NOTE: The assignment said that generate(1) should return an empty list, but that doesn't make sense,
                because 1 is a factor of itself. So for prime n, generate(n) == [n]
        */
        ArrayList<Integer> retVal = new ArrayList<Integer>();

        //highest possible factor is half of the original number
        int end = n/2;
        for (int i = 2; i <= end; i++) {
            if (n % i == 0) {
                //add the found factor to the list, divide it out of n, and be sure to check for repeated factors
                retVal.add(i);
                n /= i;
                i--;
            }
        }
        //handle prime numbers
        if (retVal.isEmpty())
            retVal.add(n);
        return retVal;
    }
}
